package com.training.action.liuyong;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.training.pojo.chengshu.UUser;
import com.training.service.liuyong.IAjaxService;

public class AjaxCheckAction extends ActionSupport {

	private String username;
	private String password;
	private String role;
	private String code;
	private UUser loginUser;
	private Map<String, Object> session;
	private boolean checkLoginResult = false; // 标记登录成功与否
	private boolean codeResult = true; // 标记验证码是否相等

	private IAjaxService ajaxService;

	public String execute() {

		System.out.println("Username = " + username + "Password = " + password
				+ "Role = " + role);
		String codeRandom = (String)ActionContext.getContext().getSession().get("codeRandom");
		if (!codeRandom.equalsIgnoreCase(code)) {
			this.codeResult = false;
			System.out.println("验证码错误。。。。" + code);
			return SUCCESS;
		}
		System.out.println("验证码正确.....");
		
		checkLoginResult = ajaxService.checkLoginService(username, password);
		// 如果登录成功，就向Session里面放置用户信息
		if (this.checkLoginResult) {
			ActionContext actionContext = ActionContext.getContext();
			this.session = actionContext.getSession();
			// 获取已登录成功的用户信息
			this.loginUser = ajaxService.getLoginUser(username, password);
			this.session.put("loginUser", this.loginUser);
		}
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isCheckLoginResult() {
		return checkLoginResult;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public IAjaxService getAjaxService() {
		return ajaxService;
	}

	public void setAjaxService(IAjaxService ajaxService) {
		this.ajaxService = ajaxService;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public boolean isCodeResult() {
		return codeResult;
	}
}
